<?php require_once('./header.php');
function rep($type) {
    $replace = array(
        '{{foreach:news}}'=>'{{foreach:comic_news:reverse}}',
        '{{endeach:news}}'=>'{{endeach:comic_news}}',
        '{{foreach:main}}'=>'{{foreach:'.$type.'}}',
        '{{endeach:main}}'=>'{{endeach:'.$type.'}}',
        '{{endeach}}'=>'{{endeach:'.$type.'}}',
        '{!eval {{bool_comic_title}}?{{!ev_comic_title}}:!:Untitled Comic!}'=>'{{if:comic_title}}{{comic_title}}{{else}}Untitled Comic{{endif}}',
        '<p>{{comic_blurb}}</p>'=>'{{comic_blurb:rich}}',
        '{{comic_blurb}}'=>'{{comic_blurb:rich}}',
        '<p>{{news_post}}</p>'=>'{{news_post:rich}}',
        '{{news_post}}'=>'{{news_post:rich}}',
        '{{import:dropdownform|{{comic_id}}}}'=>'{{plugin:dropdownform}}',
        '{{import:navbar|{{comic_id}}}}'=>'<div class="navbar">
        {{if:prev_comic}}
            <a href="http:///?id={{first_comic_id}}" title="{{first_comic_title}}">First</a>
            | <a href="http:///?id={{prev_comic_id}}" title="{{prev_comic_title}}">Prev</a>
        {{else}}
            First | Prev
        {{endif}}
        |
        {{if:next_comic}}
            <a href="http:///?id={{next_comic_id}}" title="{{next_comic_title}}">Next</a>
            | <a href="http:///?id={{last_comic_id}}" title="{{last_comic_title}}">Last</a>
        {{else}}
            Next | Last
        {{endif}}
    </div>',
        '_username'=>'_author_name',
    );
    return $replace;
}
function templatefile($t,$type=false) {
    $content = file('./storage/templates/'.$t.'.tpl');
    if ($type) {
        array_pop($content);
        array_shift($content);
    }
    return strtr(implode($content),rep($type));
}


$templatedb = load_db('template');
$globals = $templatedb->get_globals();
$globals['header']['content'] = $_POST['header'];
$globals['footer']['content'] = $_POST['footer'];
$templatedb->save_globals($globals);


$templatedb->save(array('content'=>$_POST['main']),'index',1); // Main
$templatedb->save(array('content'=>$_POST['comic']),'index',2); // Individual Comic
$templatedb->save(array('content'=>templatefile('archive','comic')),'archive',1); // Archive
$templatedb->save(array('content'=>templatefile('newsarchive','news')),'news',1); // News Archive
$templatedb->save(array('content'=>$_POST['post']),'news',2); // News Post
$templatedb->save(array('content'=>$_POST['css']),'inc/css',1); // CSS


?>
        <h1>And that's the templates all done</h1>
        
<?php if (function_exists('gd_info')) { ?>
        <h2>One more automated thing to do now</h2>
        <p><a href="thumbs.php">Click here to start converting comic images</a></p>
<?php } else { ?>
        <h2>OK, nearly there</h2>
        <p>Pretty much done now, just follow this link to finalize everything and you'll be on your way. <a href="finish.php">Off we go</a></p>
<?php } ?>
        
    </div>
</body>
</html>